package com.lechuang.order.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置绑定交换机和队列
 * @author hefei
 * @version 1.0
 * @date 2023/7/12 3:24 下午
 */
@Configuration
public class MqBindingConfig {

    /**
     * <p>
     *    普通队列
     * </p>
     * @author liaoningbo
     * @since 2023/12/25
     */
    @Bean(name = MqConstant.MQ_EXCHANGE)
    public CustomExchange exchange() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstant.MQ_EXCHANGE, "x-delayed-message",true, false,args);
    }
    @Bean(MqConstant.MQ_QUEUE)
    public Queue queue() {
        return new Queue(MqConstant.MQ_QUEUE, true, false, false);
    }
    @Bean
    public Binding queueBinding(@Qualifier(value = MqConstant.MQ_EXCHANGE) CustomExchange customExchange,
                                       @Qualifier(value = MqConstant.MQ_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue)
                .to(customExchange).with("").noargs();
    }

    /**
     * <p>
     *    延迟队列
     * </p>
     * @author liaoningbo
     * @since 2023/12/25
     */
    @Bean(name = MqConstant.MQ_DELAYED_EXCHANGE)
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstant.MQ_DELAYED_EXCHANGE, "x-delayed-message",true, false,args);
    }
    @Bean(MqConstant.MQ_DELAYED_QUEUE)
    public Queue delayedQueue() {
        return new Queue(MqConstant.MQ_DELAYED_QUEUE, true, false, false);
    }
    @Bean
    public Binding delayedQueueBinding(@Qualifier(value = MqConstant.MQ_DELAYED_EXCHANGE) CustomExchange customExchange,
                                       @Qualifier(value = MqConstant.MQ_DELAYED_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue)
                .to(customExchange).with("").noargs();
    }

    /**
     * <p>
     *     广播
     * </p>
     * @author liaoningbo
     * @since 2023/12/25
     */
    @Bean(name = MqConstant.MQ_BROADCAST_EXCHANGE)
    public FanoutExchange broadcastExchange() {
        return new FanoutExchange(MqConstant.MQ_BROADCAST_EXCHANGE, true, false);
    }
    @Bean(MqConstant.MQ_BROADCAST_QUEUE1)
    public Queue broadcastQueue1() {
        return new Queue(MqConstant.MQ_BROADCAST_QUEUE1, true, false, false);
    }
    @Bean
    public Binding broadcastQueue1Binding(@Qualifier(value = MqConstant.MQ_BROADCAST_EXCHANGE) FanoutExchange fanoutExchange,
                                          @Qualifier(value = MqConstant.MQ_BROADCAST_QUEUE1) Queue queue) {
        return BindingBuilder.bind(queue)
                .to(fanoutExchange);
    }

    @Bean(MqConstant.MQ_BROADCAST_QUEUE2)
    public Queue broadcastQueue2() {
        return new Queue(MqConstant.MQ_BROADCAST_QUEUE2, true, false, false);
    }
    @Bean
    public Binding broadcastQueue2Binding(@Qualifier(value = MqConstant.MQ_BROADCAST_EXCHANGE) FanoutExchange fanoutExchange,
                                          @Qualifier(value = MqConstant.MQ_BROADCAST_QUEUE2) Queue queue) {
        return BindingBuilder.bind(queue)
                .to(fanoutExchange);
    }
}
